package com.hfh.main.matrix.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfh.main.common.annotate.JwtToken;
import com.hfh.main.common.resp.Result;
import com.hfh.main.group.entity.GroupDetailEntity;
import com.hfh.main.group.entity.GroupEntity;
import com.hfh.main.matrix.entity.WeekMatrixEntity;
import com.hfh.main.matrix.entity.WeekMatrixEnum;
import com.hfh.main.matrix.service.WeekMatrixService;
import com.hfh.main.weight.entity.ScoreWeightEntity;
import com.hfh.main.weight.mapper.ScoreWeightMapper;
import com.hfh.main.weight.service.ScoreWeightService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 分值权重
 */
@Tag(name = "矩阵")
@RestController
@RequestMapping("/weekMatrixEntity")
public class WeekMatrixController {

    @Autowired
    private WeekMatrixService weekMatrixService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @JwtToken
    @Operation(summary = "矩阵查询-学号-周")
    @PostMapping("/selectMatrixByAcc/{param}/{week}")
    public Result<?> selectMatrixByAcc(@PathVariable @Validated String param, @PathVariable @Validated int week) {
        LambdaQueryWrapper<WeekMatrixEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeekMatrixEntity::getAcc, param);
        queryWrapper.eq(WeekMatrixEntity::getWeekNumber, week);
//        List<WeekMatrixEntity> list = weekMatrixService.list(queryWrapper);
//        if (CollectionUtil.isEmpty(list)) {
//            return Result.success();
//        }
//        WeekMatrixEntity weekMatrixEntity = list.stream().findFirst().get();
//        double matrix1Score = list.stream().mapToDouble(WeekMatrixEntity::getMatrix1Score).average().getAsDouble();
//        double matrix2Score = list.stream().mapToDouble(WeekMatrixEntity::getMatrix2Score).average().getAsDouble();
//        double matrix3Score = list.stream().mapToDouble(WeekMatrixEntity::getMatrix3Score).average().getAsDouble();
//        double matrix4Score = list.stream().mapToDouble(WeekMatrixEntity::getMatrix4Score).average().getAsDouble();
//        double matrix5Score = list.stream().mapToDouble(WeekMatrixEntity::getMatrix5Score).average().getAsDouble();
//        double weekScore = list.stream().mapToDouble(WeekMatrixEntity::getWeekScore).average().getAsDouble();

        WeekMatrixEntity one = weekMatrixService.getOne(queryWrapper);
        if (one == null) {
            return Result.success();
        }
        double matrix1Score = one.getMatrix1Score();
        double matrix2Score = one.getMatrix2Score();
        double matrix3Score = one.getMatrix3Score();
        double matrix4Score = one.getMatrix4Score();
        double matrix5Score = one.getMatrix5Score();
        double weekScore = one.getWeekScore();

        WeekMatrixEnum weekMatrixEnum = WeekMatrixEnum.of(one.getIdentity());
        String matrix1Name = weekMatrixEnum.getMatrix1Name();
        String matrix2Name = weekMatrixEnum.getMatrix2Name();
        String matrix3Name = weekMatrixEnum.getMatrix3Name();
        String matrix4Name = weekMatrixEnum.getMatrix4Name();
        String matrix5Name = weekMatrixEnum.getMatrix5Name();

        WeekMatrixEntity build = WeekMatrixEntity.builder()
                .matrix1Name(matrix1Name).matrix2Name(matrix2Name).matrix3Name(matrix3Name).matrix4Name(matrix4Name).matrix5Name(matrix5Name)
                .matrix1Score(matrix1Score).matrix2Score(matrix2Score).matrix3Score(matrix3Score).matrix4Score(matrix4Score).matrix5Score(matrix5Score)
                .acc(one.getAcc()).identity(one.getIdentity())
                .matrixNames(Arrays.asList(matrix1Name, matrix2Name, matrix3Name, matrix4Name, matrix5Name))
                .matrixScores(Arrays.asList(matrix1Score, matrix2Score, matrix3Score, matrix4Score, matrix5Score))
                .weekScore(weekScore)
                .build();

        return Result.success(build);
    }


    @JwtToken
    @Operation(summary = "矩阵查询-周-学号")
    @PostMapping("/selectMatrixByWeek")
    public Result<?> selectMatrixByWeek(@RequestParam Integer week, @RequestParam String acc) {
        LambdaQueryWrapper<WeekMatrixEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeekMatrixEntity::getAcc, acc);
        queryWrapper.eq(WeekMatrixEntity::getWeekNumber, week);
        List<WeekMatrixEntity> list = weekMatrixService.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return Result.success();
        }
        WeekMatrixEntity weekMatrixEntity = list.stream().findFirst().get();
        double matrix1Score = weekMatrixEntity.getMatrix1Score();
        double matrix2Score = weekMatrixEntity.getMatrix2Score();
        double matrix3Score = weekMatrixEntity.getMatrix3Score();
        double matrix4Score = weekMatrixEntity.getMatrix4Score();
        double matrix5Score = weekMatrixEntity.getMatrix5Score();

        WeekMatrixEnum weekMatrixEnum = WeekMatrixEnum.of(weekMatrixEntity.getIdentity());
        String matrix1Name = weekMatrixEnum.getMatrix1Name();
        String matrix2Name = weekMatrixEnum.getMatrix2Name();
        String matrix3Name = weekMatrixEnum.getMatrix3Name();
        String matrix4Name = weekMatrixEnum.getMatrix4Name();
        String matrix5Name = weekMatrixEnum.getMatrix5Name();

        weekMatrixEntity
                .setMatrix1Name(matrix1Name).setMatrix2Name(matrix2Name).setMatrix3Name(matrix3Name).setMatrix4Name(matrix4Name).setMatrix5Name(matrix5Name)
                .setMatrix1Score(matrix1Score).setMatrix2Score(matrix2Score).setMatrix3Score(matrix3Score).setMatrix4Score(matrix4Score).setMatrix5Score(matrix5Score)
                .setAcc(weekMatrixEntity.getAcc()).setIdentity(weekMatrixEntity.getIdentity())
                .setMatrixNames(Arrays.asList(matrix1Name, matrix2Name, matrix3Name, matrix4Name, matrix5Name))
                .setMatrixScores(Arrays.asList(matrix1Score, matrix2Score, matrix3Score, matrix4Score, matrix5Score));

        return Result.success(weekMatrixEntity);
    }


}
